<?php
namespace app\api\controller;

use app\api\model\User as UserModel;

class User
{

    /*
     *  用户管理
     *  全站使用post
     *  2018-12-13
     *  add(添加) edit(修改）index(列表) delete(删除)
     *  'code'  http状态  msg 返回数据提示  result 成功ok 失败 error,
     *
     */

    //   添加用户
    public function add()
    {
        if (!($_POST)) {

            return json([
                'code' => 400,
                'msg' => '请求错误,请使用POST',
            ]);
        } else {
            $user['userid'] = $_POST['userid']; //用户ID
            $user['username'] = $_POST['username']; //用户名
            $user['password'] = $_POST['password']; //密码
            $user['phone'] = $_POST['phone']; //手机号
            $user['deptid'] = $_POST['deptid']; //部门id
            $user['roleid'] = $_POST['roleid']; //权限id
            $user['token'] = strtotime(date('Y-m-d H:i:s', time())); //token
            $user['addtime'] = strtotime(date('Y-m-d H:i:s', time())); //添加时间
            $user['addperson'] = 'admin'; //$_POST['addperson']; //添加人
            $result = UserModel::create($user);
            if ($result) {
                return json([
                    'result' => 'ok',
                    'msg' => '添加成功',
                ]);

            } else {

                return json([
                    'result' => 'error',
                    'msg' => '添加失败',
                ]);
            }
        }
    }

    //   修改用户
    public function edit()
    {
        if (!($_POST)) {

            return json([
                'code' => 400,
                'msg' => '请求错误,请使用POST',
            ]);

        } else {

            $id = $_POST['userid']; //用户ID
            if ($id == "") {

                return json([
                    'code' => 400,
                    'msg' => 'id没传呀',
                ]);
            }
            $user = UserModel::get($id);
            if ($user == "") {

                return "id 错误";
            }
            $user['userid'] = $user['userid']; //用户ID
            $user['username'] = $_POST['username'] != "" ? $_POST['username'] : $user->username; //用户名
            $user['password'] = $_POST['password'] != "" ? $_POST['password'] : $user->password; //密码
            $user['phone'] = $_POST['phone'] != "" ? $_POST['phone'] : $user->phone; //手机号
            $user['deptid'] = $_POST['deptid'] != "" ? $_POST['deptid'] : $user->deptid; //部门id
            $user['roleid'] = $_POST['roleid'] != "" ? $_POST['roleid'] : $user->roleid; //权限id
            $user['token'] = time(); //token
            $user['addtime'] = date('Y-m-d', time()); //添加时间
            $user['addperson'] = $_POST['addperson'] != "" ? $_POST['addperson'] : $user->addperson; //添加人

            $res = $user->save();

            if ($res) {

                return json([
                    'result' => 'ok',
                    'msg' => '更新成功',
                ]);

            } else {

                return json([
                    'result' => 'error',
                    'msg' => '更新失败',
                ]);
            }
        }
    }

    // 获取列表
    public function index()
    {

        $list = UserModel::all();
        return json(['result' => 'ok', 'data' => $list]);
    }

    // 删除数据
    public function delete()
    {
        $id = $_POST['userid']; //用户ID

        if (!($_POST)) {
            return json([
                'code' => 400,
                'msg' => '请求错误,请使用POST',
            ]);
        } else {

            if ($id == "") {
                return json([
                    'code' => 400,
                    'msg' => 'id没传呀',
                ]);
            }

            $user = UserModel::get($id);

            if ($user) {
                $user->delete();
                return json([
                    'result' => 'ok',
                    'msg' => '删除用户成功',
                ]);

            } else {
                return json([
                    'result' => 'error',
                    'msg' => '删除用户失败',
                ]);
            }
        }
    }

}
